package com.onemileshop.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlSerializer;

import com.onemileshop.entiy.Session;
import com.onemileshop.entiy.model.EMsgHead;
import com.onemileshop.entiy.model.EPostData;

import android.util.Log;
import android.util.Xml;

public class UtiNet
{
   
   private static int CONNECTION_TIMEOUT=30000;
   private static int SO_TIMEOUT=30000;
   
   /**
    * 
    * @param url
    * @param postData
    * @param callBack
    * @return
    */
   public static boolean HttpPostData(String url, EMsgHead head, ArrayList<EPostData> list, PostCallBack callBack)
   {
	  DefaultHttpClient client = new DefaultHttpClient();
	  HttpPost post = new HttpPost(url);
	  StringEntity se=null;
	  try
	  {
		 se = new StringEntity(BuilderPostData(head, list),HTTP.UTF_8);
	     post.setEntity(se); 
		 HttpResponse response = client.execute(post);
		 if (response.getStatusLine().getStatusCode() == 200)// 成功
		 {
			callBack.DoWhenPostComplete(response);
			return true;
		 }
		 else
		 {
			Log.e("ERROR", "UtiNet_HttpPostData()--" + response.getStatusLine().toString());
	        return false;
		 }
	  }
	  catch (UnsupportedEncodingException e1)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e1);
		 }
	     return false;
	  }
	  catch (ClientProtocolException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
		 return false;
	  }
	  catch (IOException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
	     return false;
	  }
   }
   
   /**
    * 
    * @param url
    * @param postData
    * @param callBack
    * @return
    */
   public static HttpResponse HttpPostData2(String url, EMsgHead head, ArrayList<EPostData> list)
   {
	  DefaultHttpClient client = new DefaultHttpClient();
	  HttpPost post = new HttpPost(url);
	  StringEntity se=null;
	  try
	  {
		 HttpConnectionParams.setConnectionTimeout(client.getParams(), CONNECTION_TIMEOUT);
		 HttpConnectionParams.setSoTimeout(client.getParams(), SO_TIMEOUT);
		 
		 se = new StringEntity(BuilderPostData(head, list),HTTP.UTF_8);
	     post.setEntity(se); 
		 HttpResponse response = client.execute(post);
		 if (response.getStatusLine().getStatusCode() == 200)// 成功
		 {
			return response;
		 }
		 else
		 {
			Log.e("ERROR", "UtiNet_HttpPostData()--" + response.getStatusLine().toString());
	        return null;
		 }
	  }
	  catch (UnsupportedEncodingException e1)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e1);
		 }
	     return null;
	  }
	  catch (ClientProtocolException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
		 return null;
	  }
	  catch (IOException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_HttpPostData(url, postData) ", e);
		 }
	     return null;
	  }
   }
   
   /**
    * 
    * @param _url
    * @return
    */
   public static InputStream GetInputStream(String _url)
   {
	  try
	  {
		 URL url = new URL(_url);
		 return url.openConnection().getInputStream();
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiNet_GetInputStream(_url) ", e);
		 }
	  }
	  return null;
   }
   
//   public static boolean HttpGetData(String _url, GetCallBack callBack)
//   {
//	  try
//	  {  URL url = new URL(_url);
//		 callBack.DoWhenGetComplete(url.openConnection().getInputStream());
//		 return true;
//	  }
//	  catch (Exception e)
//	  {
//		 if (Session.getInstance().IsDEBUG())
//		 {
//			Log.e("ERROR", "UtiNet_HttpGetData(url, callBack) ", e);
//		 }
//		 return false;
//	  }
//   }
//   public interface GetCallBack
//   {
//	 public void DoWhenGetComplete(InputStream is);
//   }
   
   
   public static String BuilderPostData(EMsgHead head,ArrayList<EPostData> list)
   {
	  try
	  {
		 XmlSerializer xs = Xml.newSerializer();
		 StringWriter sw=new StringWriter();
		 xs.setOutput(sw);
		 xs.startDocument("UTF-8", null);
		 xs.startTag(null, "Message");
		   xs.startTag(null,"Head");
		       xs.startTag(null,"Version");
		       xs.text(head.getVersion()==null?"":head.getVersion());
		       xs.endTag(null, "Version");

		       xs.startTag(null,"PostTime");
		       xs.text(UtiDateTime.ToDateTime(head.getPostTime()));
		       xs.endTag(null, "PostTime");

		       xs.startTag(null,"APICode");
		       xs.text(head.getAPICode());
		       xs.endTag(null, "APICode");

		       xs.startTag(null, "PageNo");
		       xs.text(String.valueOf(head.getPageNo()));
		       xs.endTag(null, "PageNo");

		       xs.startTag(null,"PageSize");
		       xs.text(String.valueOf(head.getPageSize()));
		       xs.endTag(null, "PageSize");

		       xs.startTag(null, "PageCount");
		       xs.text(String.valueOf(head.getPageCount()));
		       xs.endTag(null,"PageCount");

		       xs.startTag(null,"UseGZip");
		       xs.text(String.valueOf(head.getUseGZip()));
		       xs.endTag(null,"UseGZip");

		       xs.startTag(null,"RspCode");
		       xs.text(head.getRspCode()==null?"":head.getRspCode());
		       xs.endTag(null,"RspCode");

		       xs.startTag(null,"RspMsg");
		       xs.text(head.getRspMsg()==null?"":head.getRspMsg());
		       xs.endTag(null,"RspMsg");
		     xs.endTag(null, "Head");
		     
		     xs.startTag(null, "Body");
		       int size=list.size();   
		       for(int i=0;i<size;i++)
		       {
		    	 xs.startTag(null,list.get(i).getKey());
		    	 xs.text(list.get(i).getValue());
		    	 xs.endTag(null, list.get(i).getKey());
		       }
		     xs.endTag(null, "Body");
		   xs.endTag(null, "Message");
		xs.endDocument();
		
		return sw.toString();

	  }
	  catch (IllegalArgumentException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiPostMsgBuilder()_BuilderPostData ", e);
		 }
	  }
	  catch (IllegalStateException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiPostMsgBuilder()_BuilderPostData ", e);
		 }
	  }
	  catch (IOException e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "UtiPostMsgBuilder()_BuilderPostData ", e);
		 }
	  }
	  return null;
   }
   
   public interface PostCallBack
   {
	  public void DoWhenPostComplete(HttpResponse response);
   }
}
